package com.xintonghua.bussiness;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.GetCallback;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.xintonghua.base.ui.BaseActivity;
import com.xintonghua.base.ui.MySubscriber;
import com.xintonghua.bussiness.api.manager.AuthManager;
import com.xintonghua.bussiness.bean.CheckBean;
import com.xintonghua.bussiness.ui.DownApkActivity;
import com.xintonghua.bussiness.ui.MainActivity;
import com.xintonghua.bussiness.ui.MyWebViewActivity;
import com.xintonghua.bussiness.ui.user.LoginActivity;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.Call;
import okhttp3.Response;


public class WelcomeActivity extends BaseActivity {
    private final int defaultTime = 2;
    @BindView(R.id.iv_welcome)
    ImageView ivWelcome;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        ButterKnife.bind(this);
        //getYApi();
        getDataByLeancloudCd();
        ini();
    }

    public void ini() {

            //PlatformConfig.setSinaWeibo("2604761814", "fa8691898f6cfbf71a1fd28dfea9ab3a", "http://sns.whalecloud.com");
        try {
            JSONArray js = new JSONArray();
            JSONObject object = new JSONObject();
            object.put("choose", "A");
            object.put("value", "10");
            object.put("score", "1");
            JSONObject object1 = new JSONObject();
            object1.put("choose", "B");
            object1.put("value", "20");
            object1.put("score", "2");

            js.put(object);
            js.put(object1);
            Log.i("aaa", "json=====>" + js.toString());


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
//    private void getYApi() {
//        OkHttpUtils.get()
//                .url("https://mbk.mynatapp.cc/web/user/getAppMsg/b99c06eb578b4277bb5b17089fa284cf")
//                .build().execute(new StringCallback() {
//            @Override
//            public void onError(Call call, Response response, Exception e, int id) {
//                goNormalWay();
//            }
//
//            @Override
//            public void onResponse(String response, int id) {
//                CheckBean bean = com.alibaba.fastjson.JSONObject.parseObject(response, CheckBean.class);
//                if (bean.getStatus() == 0) {
//                    if (bean.getResult() == null) {
//                        goNormalWay();
//                    } else {
//                        if (bean.getResult().getVs().equals("4")) {//跳转到网页  取url的链接
//                            String wapurl = bean.getResult().getUrl();  // webview加载H5
//                            Intent intent = new Intent(WelcomeActivity.this, MyWebViewActivity.class);
//                            intent.putExtra("url", wapurl);
//                            startActivity(intent);
//                            finish();
//                        } else if (bean.getResult().getVs().equals("5")) {// 跳转强更  去ud的强更地址
//                            String upUrl = bean.getResult().getUd();  // 强更地址
//                            Intent intent = new Intent(WelcomeActivity.this, DownApkActivity.class);
//                            intent.putExtra("url", upUrl);
//                            startActivity(intent);
//                            finish();
//                        } else {
//                            goNormalWay();
//                        }
//                    }
//                } else {
//                    goNormalWay();
//                }
//
//                finish();
//            }
//        });
//
//    }
    /**
     * 通过Leancloud控制
     */
    private void getDataByLeancloudCd() {
        AVQuery<AVObject> avQuery = new AVQuery<>("switch");
        avQuery.getInBackground("5dfb2b52f7308e00087bb434", new GetCallback<AVObject>() {

            @Override
            public void done(AVObject avObject, AVException e) {
                if (avObject != null) {
                    if (avObject.getBoolean("openUrl")) {//开关判断，是否打开H5。如果为true
                        String wapurl = avObject.getString("url");  // webview加载H5
                        Intent intent = new Intent(WelcomeActivity.this, MyWebViewActivity.class);
                        intent.putExtra("url", wapurl);
                        startActivity(intent);
                        finish();
                    } else if (avObject.getBoolean("openUp")) {//开关判断，是否打开。如果为true
                        String upUrl = avObject.getString("urlUp");  // 强更地址
                        Intent intent = new Intent(WelcomeActivity.this, DownApkActivity.class);
                        intent.putExtra("url", upUrl);
                        startActivity(intent);
                        finish();
                    } else {
                        goNormalWay();
                    }
                } else {
                    goNormalWay();
                }
                finish();
            }
        });
    }

    private void goNormalWay() {
        timer();
    }


    /**
     * 定时器
     */
    private void timer() {
        if (AuthManager.geteAuth() == null) {
            openActivity(LoginActivity.class);
        } else {
            openActivity(MainActivity.class);
            finish();
        }
//        Flowable.timer(defaultTime, TimeUnit.SECONDS)
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new MySubscriber<Long>() {
//                    @Override
//                    public void next(Long aLong) {
//
//
//                    }
//                });


    }
}
